package Controller;

import java.beans.XMLDecoder;
import java.beans.XMLEncoder;
import java.io.*;
import java.util.ArrayList;

/**
 * @author Matthew
 *         Date: Oct 24, 2010
 *         Time: 1:11:51 PM
 */
public class FileHandler {
    private ArrayList openObjects;
    private Boolean eof;

    public void save(ArrayList objectArray, String fileName) throws FileNotFoundException {
        XMLEncoder e = new XMLEncoder(
                new BufferedOutputStream(
                        new FileOutputStream(fileName + ".xml")));
        for (int i = 0; i < objectArray.size(); i++) {
            Object o = objectArray.get(i);
            e.writeObject(o);
        }
        e.close();
    }

    public ArrayList open(String fileName) throws FileNotFoundException {
        openObjects = new ArrayList();
        XMLDecoder d = new XMLDecoder(
                new BufferedInputStream(
                        new FileInputStream(fileName + ".xml")));
        eof = false;
        while (!eof){
            try {
                Object result = d.readObject();
                openObjects.add(result);
            } catch (ArrayIndexOutOfBoundsException e){
                eof = true;
            }
        }
        d.close();
        return openObjects;
    }

}
